#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import cgi
import wsgiref.handlers
import os
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.api import images

import map_sam
import jquery_sam
import bigtable_sam
import wariogame

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  avatar = db.BlobProperty()
  thumb = db.BlobProperty()
  date = db.DateTimeProperty(auto_now_add=True)

class MainHandler(webapp.RequestHandler):

  def get(self):
    template_values = {'no' : 'no'}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class Image (webapp.RequestHandler):
  def get(self):
    greeting = db.get(self.request.get("img_id"))
    if greeting.avatar:
      self.response.headers['Content-Type'] = "image/png"
      self.response.out.write(greeting.thumb)
    else:
      self.response.out.write("No image")

class Guestbook(webapp.RequestHandler):
  def post(self):
    imgArray = ["img1", "img2", "img3", "img4", "img5"]
    for eachimg in imgArray:
      greeting = Greeting()
      avatar = self.request.get(eachimg)
      thumb = images.resize(avatar, 60, 60)
      greeting.avatar = db.Blob(avatar)
      greeting.thumb = db.Blob(thumb)
      greeting.put()
    self.redirect('/bigtable')

class Createbook(webapp.RequestHandler):
  def get(self):
    greeting = Greeting()
    greeting.content = 'init big table'
    greeting.put()
    self.redirect('/bigtable')

def main():
  application = webapp.WSGIApplication([('/', MainHandler),
					('/sign', Guestbook),
					('/img', Image),
					('/create_bigtable', Createbook),
					('/map', map_sam.MapHandler),
					('/jquery', jquery_sam.JqueryHandler),
					('/bigtable', bigtable_sam.BigtableHandler),
					('/wario', wariogame.WariogameHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
